package com.demo.api.config;


import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

/**
 * @author Tan-Bowen
 * @description TODO 自定义json转换器，取代默认的转换器
 * @date 2021/7/20 17:11
 */
@Configuration
public class JacksonConfig {

    /**
     * @author Tan-Bowen
     * @description TODO Jackson全局转化long类型为String，解决jackson序列化时long类型缺失精度问题
     * @date 2021/7/26 17:47
     * @return org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer
     */
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return new Jackson2ObjectMapperBuilderCustomizer() {
            @Override
            public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
                jacksonObjectMapperBuilder
                        .serializerByType(Long.TYPE, ToStringSerializer.instance)
                        .serializerByType(Long.class,ToStringSerializer.instance)
                ;
            }
        };
    }

}
